package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.UserLike;
import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;


import java.util.List;

@DubboService
public class FriendsApiImpl implements FriendsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //1 互相喜欢
    @Override
    public List<UserLike> queryfriendsList(Integer page, Integer pagesize, Long userId) {

        //获取喜欢
        List<UserLike> LoveId = mongoTemplate
                .find(Query.query(Criteria
                        .where("userId")
                        .is(userId)), UserLike.class);
        List<ObjectId> likeUserId = CollUtil.getFieldValues(LoveId, "id", ObjectId.class);
        //获取我关注的用户ID
        Long likeUserId1 = mongoTemplate.findById(likeUserId, UserLike.class).getLikeUserId();
        List<UserLike> eachLoves = mongoTemplate
                .find(Query.query(Criteria
                        .where("likeUserId")
                        .is(userId).and("userId").is(likeUserId1)), UserLike.class);
        return eachLoves;
    }


    //2 获取我关注列表
    @Override
    public List<UserLike> queryLovesList(Integer page, Integer pagesize, Long userId) {

        List<Friend> friends = mongoTemplate.find(Query.query(Criteria.where("userId").ne(userId)), Friend.class);
        List<Long> friendsId = CollUtil.getFieldValues(friends, "userId",Long.class);
        List<UserLike> list = mongoTemplate.find(
                Query.query(Criteria.where("userId")
                        .is(userId).and("isLike").is(true).and("likeUserId").nin(friendsId))
                        .limit(pagesize).skip((page - 1) * pagesize)
                        .with(Sort.by(Sort.Order.desc("created"))), UserLike.class);

        return list;
    }


    //3 获取粉丝列表
    @Override
    public List<UserLike> queryFansList(Integer page, Integer pagesize, Long userId) {
        List<Friend> friends = mongoTemplate.find(Query.query(Criteria.where("userId").ne(userId)), Friend.class);
        List<Long> friendsId = CollUtil.getFieldValues(friends, "userId",Long.class);
        Query query = new Query(Criteria.where("likeUserId")
                .is(userId).and("isLike").is(true).and("userId").nin(friendsId))
                .limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    //判断是否喜欢粉丝
    @Override
    public boolean fansLike(Long userId, Long uid) {
        return mongoTemplate.exists(Query.query(
                Criteria.where("likeUserId").is(userId)
                        .and("userId").is(uid)
                        .and("isLike").is(true)), UserLike.class);
    }

    @Override
    public void deleteFriend(Long userId, Long uid) {
        mongoTemplate.remove(Query.query(Criteria.where("friendId").is(uid)
                .and("userId").is(userId)), Friend.class);

        mongoTemplate.remove(Query.query(Criteria.where("friendId").is(userId)
                .and("userId").is(uid)), Friend.class);
    }

    //判断好友关系是否还存在
    @Override
    public boolean findFriends(Long userId, Long uid) {
        return mongoTemplate.exists(Query.query(
                Criteria.where("userId").is(userId)
                .and("friendId").is(uid)), Friend.class);
    }

}
